package com.east.service.task.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.Staff;
import com.east.bean.Task;
import com.east.bean.User;
import com.east.dao.StaffMapper;
import com.east.dao.TaskMapper;
import com.east.service.task.TaskPersonalService;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("taskPersonalService")
public class TaskPersonalServiceImpl implements TaskPersonalService {
	@Autowired
    private TaskMapper taskMapper;
	@Autowired
    private StaffMapper staffMapper;
	@Override
	public Page<Task> findTask(Pagenation pagenation, Task task,User user) {
		   Page<Task> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		   Example example = new Example(Task.class);
		   Criteria cr = example.createCriteria();
		   cr.andEqualTo("tState", task.gettState());
		   cr.andEqualTo("tDesigneeId", user.getStaff().getsId());
		   taskMapper.selectByExample(example);
		return startPage;
	}
	@Override
	public Map<String, Integer> findCount(Task task, User user) {  
		Integer getsId = user.getStaff().getsId();
			Example example = new Example(Task.class);
		   Criteria cr = example.createCriteria();
		   cr.andEqualTo("tState", 0);
		   cr.andEqualTo("tDesigneeId", getsId);
		   int selectCountByExample = taskMapper.selectCountByExample(example);
		   Example example01 = new Example(Task.class);
		   Criteria cr1 = example01.createCriteria();
		   cr1.andEqualTo("tState", 1);
		   cr1.andEqualTo("tDesigneeId", getsId);
		   int integer = taskMapper.selectCountByExample(example01);
		   Map<String,Integer> map = new HashMap<String,Integer>();
		   map.put("dwc", selectCountByExample);
		   map.put("ywc", integer);
		return map;
	}
	@Override
	public Map<String, Integer> findCountP(Task task, User user) {
		List<Staff> finbyDepartmentdId = staffMapper.finbyDepartmentdId(user.getStaff().getDepartmentId());
		
			Example example = new Example(Task.class);
		   Criteria cr = example.createCriteria();
		   cr.andEqualTo("tDepId", user.getStaff().getDepartmentId());
		   List<Task> selectByExample = taskMapper.selectByExample(example);
		   int workNum=0;
		   int maintenanceNum=0;//维护中人数
		   int projectNum=0;//项目中人数
		   int freeNum=0;//空闲人数
		   int projectTotalNum=0;//项目合计工作量
		   int maintenanceTotalNum=0;//维护合计工作量
		   
		   for (Staff sta : finbyDepartmentdId) {
			  if(!istrue(sta.getsId(), selectByExample)) {
				  freeNum+=1;
			  }
			 
		   }
		   for (Task task2 : selectByExample) {
			if(task2.gettType().equals(0)) {
				projectNum+=1;
				projectTotalNum+=task2.gettWorkload();
			}else if(task2.gettType().equals(1)) {
				maintenanceNum+=1;
				maintenanceTotalNum+=task2.gettWorkload();
			}
			workNum+=task2.gettWorkload();
		   }
		   Map<String,Integer> map = new HashMap<String,Integer>();
		   map.put("workNum", workNum);
		   map.put("maintenanceNum", maintenanceNum);
		   map.put("projectNum", projectNum);
		   map.put("freeNum", freeNum);
		   map.put("projectTotalNum", projectTotalNum);
		   map.put("maintenanceTotalNum", maintenanceTotalNum);
		return map;
	}
	public boolean istrue( Integer pid,List<Task> selectByExample) {
		for (Task task : selectByExample) {
			if(pid.equals(task.gettDesigneeId())) {
				return true;
			}
		}
		return false;
	}
	@Override
	public Page<Staff> findStaff(Pagenation pagenation,User user,Task task) {
		 Page<Staff> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		 task.settDepId(user.getStaff().getDepartmentId());
		 taskMapper.findStaff(task);
		return startPage;
	}
	@Override
	public Page<Task> findProject(Pagenation pagenation, User user) {
		Page<Task> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		 List<Task> findStaff = taskMapper.findProject(user.getStaff().getDepartmentId());
		return startPage;
	}
	@Override
	public Page<Task> findStaffX(Pagenation pagenation, User user, Task task) {
		Page<Task> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		task.settDepId(user.getStaff().getDepartmentId());
		 taskMapper.findStaffX(task);
		return startPage;
	}
			
}

